package com.example.sls.controller.account;

import com.example.sls.pojo.account.Account;
import com.example.sls.service.account.AccountService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    AccountService accountService;

    @PostMapping ("/add/{id}/{username}/{password}/{phone}/{email}/{roleId}")
    public String addAccount(@PathVariable("id") Integer id, @PathVariable("username") String username,@PathVariable("password") String password, @PathVariable("phone")String phone, @PathVariable("email")String email, @PathVariable("roleId") Integer roleId ){

//       Map<String, Object>account = new HashMap<>();
//       account.put("id", id);
//       account.put("username", username);
//       account.put("password", password);
//       account.put("roleId", roleId);
        Account account = new Account(id, username, password,phone, email, roleId);

        boolean flag = accountService.addAccount(account);
       if(flag){
           return "success";
       }
       else{
           return "failed";
       }
    }

    @PostMapping("/update")
    public Map<String,String> updateAccount(@RequestParam Map<String, String> params){

        HashMap<String, String> result = new HashMap<>();

        if(
         accountService.getAccountByUserName(params.get("username")) != null
            &&
         !params.get("username").equals(accountService.getAccountById(Integer.valueOf(params.get("id"))).getUsername())
        ){
            result.put("error_message", "用户名已存在");
            return result;
        }
        if ( params.get("username") == null) {
            result.put("error_message", "用户名不能为空");
            return result;
        }

        if ( (params.get("username")).length() == 0) {
            result.put("error_message", "用户名不能为空");
            return result;
        }

        if ( ( params.get("phone") ).length() == 0) {
            result.put("error_message", "手机号不能为空");
            return result;
        }
        if (( params.get("username") ).length() > 20) {
            result.put("error_message", "用户名长度不能大于20");
            return result;
        }

        Map<String, Object> account = new HashMap<>();
        account.put("id", Integer.parseInt(params.get("id")));
        account.put("username", params.get("username"));
        account.put("phone", params.get("phone"));
        account.put("email", params.get("email"));
        //System.out.println("account-AccountController" + account);

        boolean flag = accountService.updateAccount(account);

        if(flag){
            result.put("error_message","success");
        }
        else{
            result.put("error_message","failed");
        }
        return result;
    }

    @PostMapping("/deleteById")
    public Map<String,String> deleteAccount(@RequestParam Map<String, String> params){
        Integer id = Integer.parseInt(params.get("id"));
        boolean flag =  accountService.deleteAccount(id);
        HashMap<String, String> result = new HashMap<>();
        if(flag){
            result.put("error_message", "success");
        }
        else{
            result.put("error_message", "failed");
        }
        return result;
    }

    @GetMapping("/queryById/{id}")
    public  Account getAccountById(@PathVariable("id") Integer id){
        return accountService.getAccountById(id);
    }

    @GetMapping("/queryByUserName/{username}")
    public Account getAccountByUserName( @PathVariable("username") String  username){
        return accountService.getAccountByUserName(username);
    }

    //查询account表中的所有账号信息
    @GetMapping("/query")
    public List<Account> getAllAccount(){
        return accountService.getAllAccount();
    }



    //分页查询所有信息
    @GetMapping("/pageInfo/{pageNum}/{pageSize}")
    public PageInfo<Account> getAllAccountByPage(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize){
        return accountService.getAllAccountByPage(pageNum, pageSize);
    }
}
